package com.doordu.soa.service.web.converter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;

public class String2LocalDateConverter implements Converter<String, LocalDate> {
	private static Logger log = LoggerFactory.getLogger(String2LocalDateConverter.class);
	@Override
	public LocalDate convert(String source) {
		if(StringUtils.isBlank(source)){
			return null;
		}
		
		String pattern = "yyyy-MM-dd";
		if(source.indexOf("/") > 0){
			pattern = "yyyy/MM/dd";
		}
		try{
			LocalDate date = LocalDate.parse(source, DateTimeFormatter.ofPattern(pattern));
			return date;
		}catch(Exception ex){
			log.error("日期参数格式转换错误! source="+source);
			throw new RuntimeException(ex);
		}
	}

}
